package com.towery.dbsrcmanager.controllers;

import com.towery.dbsrcmanager.db.service.DatabaseService;
import com.towery.dbsrcmanager.db.service.Db2DatabaseService;
import com.towery.dbsrcmanager.db.service.VersionService;
import com.towery.dbsrcmanager.es.entity.TDbObjectsEsVO;
import com.towery.dbsrcmanager.mybatis.base.dao.TDatabaseDAO;
import com.towery.dbsrcmanager.mybatis.base.dao.TDbObjectsDAO;
import com.towery.dbsrcmanager.mybatis.base.entity.TDatabaseVO;
import com.towery.dbsrcmanager.mybatis.base.entity.TDbObjectsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by User on 2016/12/30.
 */
@Slf4j
@Controller
@RequestMapping(value="/base/db")
public class DBController {



    @Autowired
    TDatabaseDAO  databaseDAO;
@Autowired
    TDbObjectsDAO  tDbObjectsDAO;
    @Autowired
    VersionService versionService;


    @RequestMapping(value="/get/name/{databaseId}")
    public @ResponseBody
    String getDatabaseName(@PathVariable("databaseId")  String databaseId )  {

        TDatabaseVO  databaseVO =databaseDAO.selectById(databaseId);

        return databaseVO.getName();

    }

    @RequestMapping(value="/ddl/{databaseId}/{scema}/{tablename}")
    public @ResponseBody
    String getDDL(@PathVariable("databaseId")  String databaseId , @PathVariable("scema")   String scema , @PathVariable("tablename") String tablename)  {

        TDatabaseVO  databaseVO =databaseDAO.selectById(databaseId);

        try {
            DatabaseService  dbsevice =new Db2DatabaseService(databaseVO);

            return dbsevice.getTableDDL(scema ,tablename);
        } catch (Exception e) {
            log.error("",e);
        }

        return null;
    }

    @RequestMapping(value="/ddl/import/tmp/{databaseId}")
    public @ResponseBody
    boolean importDDLToTmp(@PathVariable("databaseId")  String databaseId )  {

        return versionService.importDDLToTmp(databaseId);
    }


    @RequestMapping(value="/objects/sync/{databaseId}")
    public @ResponseBody
    boolean sync(@PathVariable("databaseId")  String databaseId )  {

        return versionService.sync(databaseId,true);
    }


    @RequestMapping(value="/objects/syncAll")
    public @ResponseBody
    boolean syncAll( )  {

        List<Map<String,Object>> list = null;
        try {
            list = databaseDAO.getDatabaseTreeList();
        } catch (Exception e) {
            log.error("",e);
            return false;
        }
        boolean r =true;
        if(list!=null && list.size()>0){
            int i=0;
            int s =list.size();
            for(Map<String,Object> d: list){
                String databaseId =(String)d.get("id");
                try {
                    boolean submit =(i==s-1);
                    versionService.sync(databaseId,submit);
                }catch(Exception e){
                    log.error("",e);
                    r= false;
                }
                i++;
            }
        }
        return r;

    }
	
	@RequestMapping(value="/objects/get/{databaseid}/{scema}/{type}/{objectname}")
    public @ResponseBody
    TDbObjectsVO getObject(@PathVariable("databaseid")  String databaseid ,
	  @PathVariable("scema")  String scema ,
	  @PathVariable("type")  String type ,
	  @PathVariable("objectname")  String objectname 
	)  {
        try {
        return tDbObjectsDAO.getObject( databaseid, scema,type,objectname);
		} catch (Exception e) {
            log.error("",e);
        }

        return null;
    }


    //搜索对象
    @RequestMapping(value="/objects/query",method= RequestMethod.POST)
    public @ResponseBody
    Page<TDbObjectsEsVO> queryObjects( int page ,
                                    int pageSize ,
                                    String queryString
    )  {
        try {
            return versionService.getSearchResult(queryString ,page ,pageSize);
        } catch (Exception e) {
            log.error("",e);
        }

        return null;
    }
}
